package com.db.compare.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.db.compare.core.BaseResponse;
import com.db.compare.model.RbacUser;
import com.db.compare.mapper.RbacUserMapper;
import com.db.compare.service.RbacUserService;
import com.db.compare.vo.RbacUserVo;
import com.db.compare.exp.RbacUserExp;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.db.compare.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONObject;


/**
* 用户管理
* */
@Slf4j
@Controller
@RequestMapping("/rbac/user")
public class RbacUserController extends BaseController{

    @Autowired
    private RbacUserMapper rbacuserMapper;
    @Autowired
    private RbacUserService rbacuserService;

    @RequestMapping(value={"/list",""})
    public String rbacuserList(){
        return "/html/RbacUser";
    }

    @RequestMapping("/pages")
    @ResponseBody
    public PageInfo<RbacUser> pages(Model model,@RequestParam Map rbacuserMap){
        RbacUserVo rbacuserVo = JSON.parseObject(JSON.toJSONString(rbacuserMap), RbacUserVo.class);
        return rbacuserService.pageInfo(rbacuserVo);
    }

    @RequestMapping("/del")
    @ResponseBody
    public BaseResponse del(@RequestParam String ids){
        System.out.println(ids);
        String[] arrays = ids.split(",");
        rbacuserMapper.deleteByBatch(arrays);
        return new BaseResponse();
    }

    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse add(@RequestBody RbacUser rbacuser){
        rbacuser.setUpdateTime(new Date());
        if(rbacuser.getId()==null) {
            rbacuser.setCreateTime(new Date());
            rbacuserMapper.insert(rbacuser);
        }else{
            rbacuserMapper.updateByPrimaryKey(rbacuser);
        }
        return new BaseResponse(200,rbacuser);
    }
    @RequestMapping("/get")
    @ResponseBody
    public BaseResponse get(@RequestParam String id){
        int pk=Integer.parseInt(id);
        RbacUser rbacuser = rbacuserMapper.selectByPrimaryKey(pk);
        return new BaseResponse(200,rbacuser);
    }
    //下载模板
    @RequestMapping("/download")
    public void testRespExcel(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String ids=request.getParameter("ids");
        rbacuserService.download(ids,response);

    }

    @RequestMapping("/upload")
    @ResponseBody
    public BaseResponse upload(@RequestParam("file") MultipartFile file){
        if(!rbacuserService.upload(file)){
            return buildFailResponse();
        }

        return buildSuccessResponse();
    }
}